package com.pocketsweet.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.pocketsweet.R;
import com.pocketsweet.chat.ui.activity.BaseActivity;
import com.pocketsweet.service.UserService;

@SuppressLint("ResourceAsColor")
@ContentView(R.layout.dialog_screen)
public class DialogScreen extends BaseActivity implements OnClickListener {

	@ViewInject(R.id.tvSexAll)
	private TextView tvSexAll;

	@ViewInject(R.id.tvSexMale)
	private TextView tvSexMale;

	@ViewInject(R.id.tvSexFemale)
	private TextView tvSexFemale;

	@ViewInject(R.id.tvTypeAll)
	private TextView tvTypeAll;

	@ViewInject(R.id.tvTypeCentify)
	private TextView tvTypeCentify;

	@ViewInject(R.id.tvCityAll)
	private TextView tvCityAll;

	@ViewInject(R.id.tvCityNear)
	private TextView tvCityNear;

	@ViewInject(R.id.btnOk)
	private Button btnOk;
	@ViewInject(R.id.btnCancel)
	private Button btnCancel;

	private static int choosedSex = 0;// 0为全部；1为男；2为女
	private static int choosedCity = 0;// 0为全部；1同城
	private static int choosedType = 0;// 0为全部：1为只查看认证的
	private int sex = 0;

	@SuppressLint("ResourceAsColor")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);

		Intent intent = getIntent();
		if (UserService.getCurrentUser().getGender() == 1) {
			sex = 2;
		} else if (UserService.getCurrentUser().getGender() == 2) {
			sex = 1;
		}
		choosedSex = intent.getIntExtra("choosedSex", sex);
		choosedCity = intent.getIntExtra("choosedCity", 0);
		choosedType = intent.getIntExtra("choosedType", 0);

		tvSexAll.setOnClickListener(this);
		tvSexMale.setOnClickListener(this);
		tvSexFemale.setOnClickListener(this);
		tvCityAll.setOnClickListener(this);
		tvCityNear.setOnClickListener(this);
		tvTypeAll.setOnClickListener(this);
		tvTypeCentify.setOnClickListener(this);
		if (choosedSex == 0) {
			tvSexAll.setSelected(true);
		} else if (choosedSex == 1) {
			tvSexMale.setSelected(true);
		} else if (choosedSex == 2) {
			tvSexFemale.setSelected(true);
		}

		if (choosedCity == 0) {
			tvCityAll.setSelected(true);
		} else if (choosedCity == 1) {
			tvCityNear.setSelected(true);
		}

		if (choosedType == 0) {
			tvTypeAll.setSelected(true);
		} else if (choosedType == 1) {
			tvTypeCentify.setSelected(true);
		}
	}

	@OnClick(R.id.btnOk)
	public void onBtnOkClick(View v) {
		// Intent intent = getIntent();
		// intent.putExtra("choosedSex", choosedSex);
		// intent.putExtra("choosedCity", choosedCity);
		// intent.putExtra("choosedType", choosedType);
		// this.setResult(RESULT_OK, intent);
//		Log.e("=====", choosedSex + String.valueOf(choosedType) + choosedCity
//				+ " ");
//		Log.e("==Main===", Main.choosedSex + String.valueOf(Main.choosedType)
//				+ Main.choosedCity + " ");
		if (Main.choosedSex != choosedSex || Main.choosedType != choosedType
				|| Main.choosedCity != choosedCity) {
			Main.choosedSex = choosedSex;
			Main.choosedCity = choosedCity;
			Main.choosedType = choosedType;
			Intent intent2 = new Intent();
			intent2.setAction(Main.ACTION_CHOOSE_CHANGE);
			sendBroadcast(intent2);
		}
		this.finish();
	}

	@OnClick(R.id.btnCancel)
	public void onBtnCancelClick(View v) {
		// Intent intent = getIntent();
		// this.setResult(RESULT_CANCELED, intent);
		finish();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tvSexAll:
			tvSexAll.setSelected(true);
			tvSexMale.setSelected(false);
			tvSexFemale.setSelected(false);
			choosedSex = 0;
			break;
		case R.id.tvSexMale:
			tvSexAll.setSelected(false);
			tvSexMale.setSelected(true);
			tvSexFemale.setSelected(false);
			choosedSex = 1;
			break;
		case R.id.tvSexFemale:
			tvSexAll.setSelected(false);
			tvSexMale.setSelected(false);
			tvSexFemale.setSelected(true);
			choosedSex = 2;
			break;
		case R.id.tvCityAll:
			tvCityAll.setSelected(true);
			tvCityNear.setSelected(false);
			choosedCity = 0;
			break;
		case R.id.tvCityNear:
			tvCityAll.setSelected(false);
			tvCityNear.setSelected(true);
			choosedCity = 1;
			break;
		case R.id.tvTypeAll:
			tvTypeAll.setSelected(true);
			tvTypeCentify.setSelected(false);
			choosedType = 0;
			break;
		case R.id.tvTypeCentify:
			tvTypeAll.setSelected(false);
			tvTypeCentify.setSelected(true);
			choosedType = 1;
			break;
		}
	}
}
